package top.infopub.mgr.fia.service.impl;


import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.fia.dao.TaxApplyDao;
import top.infopub.mgr.fia.dao.TaxApplyDetailDao;
import top.infopub.mgr.fia.domain.TaxApplyDomain;
import top.infopub.mgr.fia.service.TaxApplyService;
import top.infopub.mgr.fia.vo.TaxApplyVo;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.model.fia.TaxApply;
import top.infopub.model.fia.TaxApplyDetail;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class TaxApplyServiceImpl extends GenericServiceImpl<TaxApply, String> implements TaxApplyService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private TaxApplyDao taxApplyDao;

    @Autowired
    private TaxApplyDetailDao taxApplyDetailDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<TaxApply, String> getDao() {
        return taxApplyDao;
    }

    @Override
    public Page<TaxApplyDomain> selectPageByDomainWhere(Page<TaxApplyDomain> page,
                                                        TaxApplyVo taxApplyVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<TaxApplyDomain>)taxApplyDao.selectByDomainWhere(taxApplyVo);
    }

    @Override
    public Page<TaxApplyDomain> selectMyCheckByDomainWhere(Page<TaxApplyDomain> page,
                                                           TaxApplyVo taxApplyVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<TaxApplyDomain>)taxApplyDao.selectMyCheckByDomainWhere(taxApplyVo);
    }

    @Override
    public TaxApplyVo selectById(String id) {
        return taxApplyDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTaxApply(TaxApplyVo taxApply, List<TaxApplyDetail> taxApplyDetails) {
        //插入预立项记录
        taxApplyDao.insertById(taxApply);

        //批量插入子项
        if (CollectionUtils.isNotEmpty(taxApplyDetails)) {
            taxApplyDetailDao.insertByBatch(taxApplyDetails);
        }

        String srcBizTable = "FIA_TAX_APPLY";
        String branchCode_xm = "from_项目发起";
        String branchCode_super = "from_总部发起";
        String hrOrgId = taxApply.getHrOrgId();// 申请公司
        // 公司类型 1=总部公司 2=区域公司 3=项目公司
        String type = orgInfoService.selectByPrimaryKey(hrOrgId).getTypes();
        String templateId = "";
        // 总部发起 
        if ("1".equals(type)) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_super);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_xm);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, taxApply.getId(), taxApply.getProcessId(), null);
    }

}